package com.zhou.common.mybatis.template.shiro;

import lombok.extern.slf4j.Slf4j;
import org.flowable.cmmn.api.CmmnRepositoryService;
import org.flowable.cmmn.api.CmmnRuntimeService;
import org.flowable.cmmn.api.CmmnTaskService;
import org.flowable.cmmn.api.repository.CaseDefinition;
import org.flowable.cmmn.api.runtime.PlanItemInstance;
import org.flowable.cmmn.engine.CmmnEngine;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

import java.util.List;

@Slf4j
//@Component
public class CMMNTest1 implements CommandLineRunner {

    @Autowired
    private CmmnEngine  cmmnEngine;
    @Override
    public void run(String... args) throws Exception {
        CmmnRepositoryService cmmnRepositoryService = cmmnEngine.getCmmnRepositoryService();
        List<CaseDefinition> caseDefinitions = cmmnRepositoryService.createCaseDefinitionQuery().list();
        System.out.println("Found " + caseDefinitions.size() + " case definitions");

        CmmnRuntimeService cmmnRuntimeService = cmmnEngine.getCmmnRuntimeService();
/*        CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
                .caseDefinitionKey("cmmn_test1")
                .variable("employee", "xianfeng")
                .start();*/

        List<PlanItemInstance> planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery()
//                .caseInstanceId(caseInstance.getId())
                .caseInstanceId("61312950-c21b-11ec-9636-5414f3dd8146")
                .orderByName().asc()
                .list();
        for (PlanItemInstance planItemInstance : planItemInstances) {
            System.out.println(planItemInstance.getName()
                    + ", state=" + planItemInstance.getState()
                    + ", parent stage=" + planItemInstance.getStageInstanceId());
        }
        List<PlanItemInstance> planItemInstances1 = cmmnRuntimeService.createPlanItemInstanceQuery()
                .caseInstanceId("61312950-c21b-11ec-9636-5414f3dd8146")
                .planItemInstanceStateActive()
                .orderByName().asc()
                .list();
        for (PlanItemInstance planItemInstance : planItemInstances1) {
            System.out.println(planItemInstance.getName()
                    + ", state=" + planItemInstance.getState()
                    + ", parent stage=" + planItemInstance.getStageInstanceId());
        }
        CmmnTaskService cmmnTaskService = cmmnEngine.getCmmnTaskService();
        List<Task> hrTasks = cmmnTaskService.createTaskQuery()
                .taskCandidateGroup("hr")
                .caseInstanceId("61312950-c21b-11ec-9636-5414f3dd8146")
                .orderByTaskName().asc()
                .list();
        for (Task task : hrTasks) {
            System.out.println("Task for HR : " + task.getName());
            cmmnTaskService.complete(task.getId());
        }

/*        hrTasks = cmmnTaskService.createTaskQuery()
                .taskCandidateGroup("hr")
                .caseInstanceId("61312950-c21b-11ec-9636-5414f3dd8146")
                .orderByTaskName().asc()
                .list();

        for (Task task : hrTasks) {
            System.out.println("Task for HR : " + task.getName());
        }*/

        List<Task> employeeTasks = cmmnTaskService.createTaskQuery()
                .caseDefinitionKey("cmmn_test1")
                .taskAssignee("xianfeng")
                .orderByTaskName().asc()
                .list();
        for (Task task : employeeTasks) {
            System.out.println("Task for employee: " + task);
        }
        employeeTasks = cmmnTaskService.createTaskQuery()
                .caseDefinitionKey("cmmn_test1")
                .taskAssignee("xianfeng")
                .taskName("拒绝工作")
                .orderByTaskName().asc()
                .list();
        for (Task task : employeeTasks) {
            System.out.println("Task for employee: " + task);
            cmmnTaskService.complete(task.getId());
        }
        employeeTasks = cmmnTaskService.createTaskQuery()
                .caseDefinitionKey("cmmn_test1")
                .taskAssignee("xianfeng")
                .orderByTaskName().asc()
                .list();
        for (Task task : employeeTasks) {
            System.out.println("Task for employee: " + task);
        }
    }
}
